/**
 * Created by jackliu on 15/12/16.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var VideoUserDoc = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  /**
   *`doc_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '文档id',
   *`doc_name` varchar(100) DEFAULT NULL COMMENT '文档名',
   *`design_id` bigint(20) NOT NULL COMMENT 'vedio_design_doc.design_id',
   *`user_id` bigint(20) NOT NULL COMMENT '用户id',
   *`purpose` int(2) NOT NULL DEFAULT '0' COMMENT '0普通文档; 1测试用; 2印品订单作品',
   *`tg_data` mediumtext COMMENT '提纲数据',
   *`hash_tag` varchar(100) DEFAULT '' COMMENT '标签',
   *`thumb` text COMMENT '缩略图',
   *`video` text COMMENT '视频地址',
   *`size_id` bigint(20) NOT NULL COMMENT '尺寸',
   *`type_id` bigint(20) NOT NULL COMMENT '分类',
   *`ver` int(10) NOT NULL DEFAULT '1' COMMENT '版本号',
   *`create_time` datetime NOT NULL,
   *`update_time` datetime NOT NULL,
   *`update_by` varchar(50) NOT NULL DEFAULT '',
   *`del_flg` int(1) NOT NULL DEFAULT '0',
   **PRIMARY KEY (`doc_id`),
   *
   * */
};
VideoUserDoc.prototype = new ItbBaseModel();

// 取得表名
VideoUserDoc.prototype.getTableName = function(){
    return 'video_user_doc';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
VideoUserDoc.prototype.getWhereConditionOfKeyEqual = function(){
    return 'doc_id='+this.doc_id;
};
//------------------------------------------------------------------

/**
 * 获取作品提纲信息(包含尺寸信息)
 *
 * @param productId 作品唯一标识
 * @param userId 用户唯一标识
 * @param callback 回调
 *              err: 错误原因
 *              data: 作品行数据 + 宽+高+单位
 * */
VideoUserDoc.prototype.getProductInfo = function(productId, userId, callback){

    pool.pool.getConnection(function(getConErr, connection) {
        if (getConErr) {
            console.log('VideoUserDoc.getProductInfo Err:'+getConErr);
            return callback(getConErr, null);
        }

        var sql =
                ' SELECT vu.*, ' +
                '        ts.width, ' +
                '        ts.height, ' +
                '        ts.unit, ' +
                '        vd.design_name ' +
                ' FROM video_user_doc vu, ' +
                '      tg_size ts, ' +
                '      video_design_doc vd ' +
                ' WHERE vu.doc_id = ' + productId +
                '   AND vu.user_id = ' + userId +
                '   AND vu.del_flg = 0 ' +
                '   AND vu.purpose = 0 ' +
                '   AND ts.size_id = vu.size_id ' +
                '   AND ts.del_flg = 0 ' +
                '   AND vd.design_id = vu.design_id ';

        console.log(sql);
        connection.query(sql, function (error, result) {
            connection.release();

            if(result && result.length>0) {
                return callback(error, result[0]);
            } else {
                return callback(error, null);
            }
        });
    });
};


// 导出
module.exports = VideoUserDoc;